package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.TeamDAO;
import com.rainng.coursesystem.dao.ActivityDAO;
import com.rainng.coursesystem.dao.MemberDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.TeamEntity;
import com.rainng.coursesystem.model.entity.ActivityEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.ActivityItemVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TeamManager extends BaseManager {
    private final ActivityDAO activityDAO;
    private final TeamDAO teamDAO;
    private final MemberDAO memberDAO;

    public TeamManager(ActivityDAO activityDAO, TeamDAO teamDAO, MemberDAO memberDAO) {
        this.activityDAO = activityDAO;
        this.teamDAO = teamDAO;
        this.memberDAO = memberDAO;
    }

    public Integer getPageCount(String areaName, String activityName, String name) {
        int count = teamDAO.count(areaName, activityName, name);
        return calcPageCount(count, TeamDAO.PAGE_SIZE);
    }

    public List<ActivityItemVO> getPage(Integer index, String areaName, String activityName, String name) {
        return teamDAO.getPage(index, areaName, activityName, name);
    }

    public TeamEntity get(Integer id) {
        return teamDAO.get(id);
    }

    public int create(TeamEntity entity) {
        return teamDAO.insert(entity);
    }

    public int update(TeamEntity entity) {
        return teamDAO.update(entity);
    }

    public int delete(Integer id) {
        return teamDAO.delete(id);
    }

    public ActivityEntity getActivityById(Integer activityId) {
        return activityDAO.get(activityId);
    }

    public boolean hasMember(Integer teamId) {
        return memberDAO.countByTeamId(teamId) > 0;
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<TeamEntity> entityList = teamDAO.listName();
        for (TeamEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
